home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / FSET2.C < prev    next >
C/C++ Source or Header  |  1992-12-08  |  18KB  |  749 lines

  1. /*
  2.  * fset2.c
  3.  *
  4.  * Compute FIRST sets for full LL(k)
  5.  *
  6.  * SOFTWARE RIGHTS
  7.  *
  8.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  9.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  10.  * company may do whatever they wish with source code distributed with
  11.  * PCCTS or the code generated by PCCTS, including the incorporation of
  12.  * PCCTS, or its output, into commerical software.
  13.  * 
  14.  * We encourage users to develop software with PCCTS.  However, we do ask
  15.  * that credit is given to us for developing PCCTS.  By "credit",
  16.  * we mean that if you incorporate our source code into one of your
  17.  * programs (commercial product, research project, or otherwise) that you
  18.  * acknowledge this fact somewhere in the documentation, research report,
  19.  * etc...  If you like PCCTS and have developed a nice tool with the
  20.  * output, please mention that you developed it using PCCTS.  In
  21.  * addition, we ask that this header remain intact in our source code.
  22.  * As long as these guidelines are kept, we expect to continue enhancing
  23.  * this system and expect to make other tools available as they are
  24.  * completed.
  25.  *
  26.  * ANTLR 1.06
  27.  * Terence Parr
  28.  * Purdue University
  29.  * 1989-1992
  30.  */
  31. #include <stdio.h>
  32. #ifdef __STDC__
  33. #include <stdarg.h>
  34. #else
  35. #include <varargs.h>
  36. #endif
  37. #include "set.h"
  38. #include "syn.h"
  39. #include "hash.h"
  40. #include "generic.h"
  41. #include "dlgdef.h"
  42.  
  43. extern char tokens[];
  44.  
  45. /* ick! globals.  Used by permute() to track which elements of a set have been used */
  46. static int *index;
  47. static set *fset;
  48. static unsigned **ftbl;
  49. static set *constrain; /* pts into fset. constrains tToken() to 'constrain' */
  50. int ConstrainSearch;
  51. static int maxk; /* set to initial k upon tree construction request */
  52. static Tree *FreeList = NULL;
  53.  
  54. #ifdef __STDC__
  55. Tree *tmake(Tree *root, ...);
  56. #else
  57. Tree *tmake();
  58. #endif
  59.  
  60. /* Do root
  61.  * Then each sibling
  62.  */
  63. void
  64. preorder(tree)
  65. Tree *tree;
  66. {
  67.     if ( tree == NULL ) return;
  68.     if ( tree->down != NULL ) fprintf(stderr, " (");
  69.     if ( tree->token == ALT ) fprintf(stderr, " J");
  70.     else fprintf(stderr, " %s", TerminalString(tree->token));
  71.     if ( tree->token==EpToken ) fprintf(stderr, "(%d)", tree->v.rk);
  72.     preorder(tree->down);
  73.     if ( tree->down != NULL ) fprintf(stderr, " )");
  74.     preorder(tree->right);
  75. }
  76.  
  77. /* check the depth of each primary sibling to see that it is exactly
  78.  * k deep. e.g.;
  79.  *
  80.  *    ALT
  81.  *   |
  82.  *   A ------- B
  83.  *   |         |
  84.  *   C -- D    E
  85.  *
  86.  * Remove all branches <= k deep.
  87.  *
  88.  * Added by TJP 9-23-92 to correct the LL(k) constraint mechanism to work.
  89.  */
  90. Tree *
  91. prune(t, k)
  92. Tree *t;
  93. int k;
  94. {
  95.     if ( t == NULL ) return NULL;
  96.     if ( t->token == ALT ) fatal("prune: ALT node in FIRST tree");
  97.     t->right = prune(t->right, k);
  98.     t->down = prune(t->down, k-1);
  99.     if ( t->down == NULL && k > 1 )
  100.     {
  101.         Tree *r = t->right;
  102.         t->right = NULL;
  103.         Tfree(t);
  104.         return r;
  105.     }
  106.     return t;
  107. }
  108.  
  109. /* build a tree (root child1 child2 ... NULL) */
  110. #ifdef __STDC__
  111. Tree *tmake(Tree *root, ...)
  112. #else
  113. Tree *tmake(va_alist)
  114. va_dcl
  115. #endif
  116. {
  117.     va_list ap;
  118.     Tree *child, *sibling=NULL, *tail;
  119. #ifndef __STDC__
  120.     Tree *root;
  121. #endif
  122. #ifdef __STDC__
  123.     require(root!=NULL, "tmake: NULL root impossible");
  124. #endif
  125.  
  126. #ifdef __STDC__
  127.     va_start(ap, root);
  128. #else
  129.     va_start(ap);
  130.     root = va_arg(ap, Tree *);
  131. #endif
  132.     child = va_arg(ap, Tree *);
  133.     while ( child != NULL )
  134.     {
  135.         if ( sibling == NULL ) sibling = tail = child;
  136.         else {tail->right = child; tail = child;}
  137.         child = va_arg(ap, Tree *);
  138.     }
  139.     root->down = sibling;
  140.     va_end(ap);
  141.     return root;
  142. }
  143.  
  144. Tree *
  145. tnode(tok)
  146. int tok;
  147. {
  148.     Tree *p, *newblk;
  149.     static int n=0;
  150.     
  151.     if ( FreeList == NULL )
  152.     {
  153.         /*fprintf(stderr, "tnode: %d more nodes\n", TreeBlockAllocSize);*/
  154.         if ( TreeResourceLimit > 0 )
  155.         {
  156.             if ( (n+TreeBlockAllocSize) >= TreeResourceLimit )
  157.             {
  158.                 fprintf(stderr, ErrHdr, FileStr[CurAmbigfile], CurAmbigline);
  159.                 fprintf(stderr, " hit analysis resource limit while analyzing alts %d and %d %s\n",
  160.                                 CurAmbigAlt1,
  161.                                 CurAmbigAlt2,
  162.                                 CurAmbigbtype);
  163.                 exit(-1);
  164.             }
  165.         }
  166.         newblk = (Tree *)calloc(TreeBlockAllocSize, sizeof(Tree));
  167.         if ( newblk == NULL )
  168.         {
  169.             fprintf(stderr, ErrHdr, FileStr[CurAmbigfile], CurAmbigline);
  170.             fprintf(stderr, " out of memory while analyzing alts %d and %d %s\n",
  171.                             CurAmbigAlt1,
  172.                             CurAmbigAlt2,
  173.                             CurAmbigbtype);
  174.             exit(-1);
  175.         }
  176.         n += TreeBlockAllocSize;
  177.         for (p=newblk; p<&(newblk[TreeBlockAllocSize]); p++)
  178.         {
  179.             p->right = FreeList;    /* add all new Tree nodes to Free List */
  180.             FreeList = p;
  181.         }
  182.     }
  183.     p = FreeList;
  184.     FreeList = FreeList->right;        /* remove a tree node */
  185.     p->right = NULL;                /* zero out ptrs */
  186.     p->down = NULL;
  187.     p->token = tok;
  188.     return p;
  189. }
  190.  
  191. static Tree *
  192. eofnode(k)
  193. int k;
  194. {
  195.     Tree *t=NULL;
  196.     int i;
  197.  
  198.     for (i=1; i<=k; i++)
  199.     {
  200.         t = tmake(tnode(EofToken), t, NULL);
  201.     }
  202.     return t;
  203. }
  204.  
  205.  
  206.  
  207. void
  208. tfree(t)
  209. Tree *t;
  210. {
  211.     if ( t!=NULL )
  212.     {
  213.         t->right = FreeList;
  214.         FreeList = t;
  215.     }
  216. }
  217.  
  218. /* tree duplicate */
  219. Tree *
  220. tdup(t)
  221. Tree *t;
  222. {
  223.     Tree *u;
  224.     
  225.     if ( t == NULL ) return NULL;
  226.     u = tnode(t->token);
  227.     u->v.rk = t->v.rk;
  228.     u->right = tdup(t->right);
  229.     u->down = tdup(t->down);
  230.     return u;
  231. }
  232.  
  233. Tree *
  234. tappend(t,u)
  235. Tree *t, *u;
  236. {
  237.     Tree *w;
  238.  
  239.     /*fprintf(stderr, "tappend(");
  240.     preorder(t); fprintf(stderr, ",");
  241.     preorder(u); fprintf(stderr, " )\n");*/
  242.     if ( t == NULL ) return u;
  243.     if ( t->token == ALT && t->right == NULL ) return tappend(t->down, u);
  244.     for (w=t; w->right!=NULL; w=w->right) {;}
  245.     w->right = u;
  246.     return t;
  247. }
  248.  
  249. /* dealloc all nodes in a tree */
  250. void
  251. Tfree(t)
  252. Tree *t;
  253. {
  254.     if ( t == NULL ) return;
  255.     Tfree( t->down );
  256.     Tfree( t->right );
  257.     tfree( t );
  258. }
  259.  
  260. /* find all children (alts) of t that require remaining_k nodes to be LL_k
  261.  * tokens long.
  262.  *
  263.  * t-->o
  264.  *     |
  265.  *     a1--a2--...--an        <-- LL(1) tokens
  266.  *     |   |        |
  267.  *     b1  b2  ...  bn        <-- LL(2) tokens
  268.  *     |   |        |
  269.  *     .   .        .
  270.  *     .   .        .
  271.  *     z1  z2  ...  zn        <-- LL(LL_k) tokens
  272.  *
  273.  * We look for all [Ep] needing remaining_k nodes and replace with u.
  274.  * u is not destroyed or actually used by the tree (a copy is made).
  275.  */
  276. Tree *
  277. tlink(t,u,remaining_k)
  278. Tree *t, *u;
  279. int remaining_k;
  280. {
  281.     Tree *p;
  282.     require(remaining_k!=0, "tlink: bad tree");
  283.  
  284.     if ( t==NULL ) return NULL;
  285.     /*fprintf(stderr, "tlink: u is:"); preorder(u); fprintf(stderr, "\n");*/
  286.     if ( t->token == EpToken && t->v.rk == remaining_k )
  287.     {
  288.         require(t->down==NULL, "tlink: invalid tree");
  289.         if ( u == NULL ) return t->right;
  290.         p = tdup( u );
  291.         p->right = t->right;
  292.         tfree( t );
  293.         return p;
  294.     }
  295.     t->down = tlink(t->down, u, remaining_k);
  296.     t->right = tlink(t->right, u, remaining_k);
  297.     return t;
  298. }
  299.  
  300. /* remove as many ALT nodes as possible while still maintaining semantics */
  301. Tree *
  302. tshrink(t)
  303. Tree *t;
  304. {
  305.     if ( t == NULL ) return NULL;
  306.     t->down = tshrink( t->down );
  307.     t->right = tshrink( t->right );
  308.     if ( t->down == NULL )
  309.     {
  310.         if ( t->token == ALT )
  311.         {
  312.             Tree *u = t->right;
  313.             tfree(t);
  314.             return u;            /* remove useless alts */
  315.         }
  316.         return t;
  317.     }
  318.  
  319.     /* (? (ALT (? ...)) s) ==> (? (? ...) s) where s = sibling, ? = match any */
  320.     if ( t->token == ALT && t->down->right == NULL)
  321.     {
  322.         Tree *u = t->down;
  323.         u->right = t->right;
  324.         tfree( t );
  325.         return u;
  326.     }
  327.     /* (? (A (ALT t)) s) ==> (? (A t) s) where A is a token; s,t siblings */
  328.     if ( t->token != ALT && t->down->token == ALT && t->down->right == NULL )
  329.     {
  330.         Tree *u = t->down->down;
  331.         tfree( t->down );
  332.         t->down = u;
  333.         return t;
  334.     }
  335.     return t;
  336. }
  337.  
  338. Tree *
  339. tflatten(t)
  340. Tree *t;
  341. {
  342.     if ( t == NULL ) return NULL;
  343.     t->down = tflatten( t->down );
  344.     t->right = tflatten( t->right );
  345.     if ( t->down == NULL ) return t;
  346.     
  347.     if ( t->token == ALT )
  348.     {
  349.         Tree *u;
  350.         /* find tail of children */
  351.         for (u=t->down; u->right!=NULL; u=u->right) {;}
  352.         u->right = t->right;
  353.         u = t->down;
  354.         tfree( t );
  355.         return u;
  356.     }
  357.     return t;
  358. }
  359.  
  360. Tree *
  361. tJunc(p,k,rk)
  362. Junction *p;
  363. int k;
  364. set *rk;
  365. {
  366.     Tree *t=NULL, *u=NULL;
  367.     Junction *alt;
  368.     Tree *tail, *r;
  369.  
  370. #ifdef DBG_TRAV
  371.     fprintf(stderr, "tJunc(%d): %s in rule %s\n", k,
  372.             decodeJType[p->jtype], ((Junction *)p)->rname);
  373. #endif
  374.     if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  375.          p->jtype==aPlusBlk || p->jtype==aSubBlk || p->jtype==aOptBlk )
  376.     {
  377.         if ( p->jtype!=aSubBlk && p->jtype!=aOptBlk ) {
  378.             require(p->lock!=NULL, "rJunc: lock array is NULL");
  379.             if ( p->lock[k] ) return NULL;
  380.             p->lock[k] = TRUE;
  381.         }
  382.         TRAV(p->p1, k, rk, tail);
  383.         if ( p->jtype==RuleBlk ) {p->lock[k] = FALSE; return tail;}
  384.         r = tmake(tnode(ALT), tail, NULL);
  385.         for (alt=(Junction *)p->p2; alt!=NULL; alt = (Junction *)alt->p2)
  386.         {
  387.             if ( tail==NULL ) {TRAV(alt->p1, k, rk, tail); r->down = tail;}
  388.             else
  389.             {
  390.                 TRAV(alt->p1, k, rk, tail->right);
  391.                 /* TJP -- lost info when tail->right was NULL
  392.                 tail = tail->right;*/
  393.                 if ( tail->right != NULL ) tail = tail->right;
  394.             }
  395.         }
  396.         if ( p->jtype!=aSubBlk && p->jtype!=aOptBlk ) p->lock[k] = FALSE;
  397. #ifdef DBG_TREES
  398.         fprintf(stderr, "blk(%s) returns:",((Junction *)p)->rname); preorder(r); fprintf(stderr, "\n");
  399. #endif
  400.         if ( r->down == NULL ) {tfree(r); return NULL;}
  401.         return r;
  402.     }
  403.  
  404.     if ( p->jtype==EndRule )
  405.     {
  406.         if ( p->halt )                        /* don't want FOLLOW here? */
  407.         {
  408.             set_orel(k, rk);                /* indicate this k value needed */
  409.             t = tnode(EpToken);
  410.             t->v.rk = k;
  411.             return t;
  412.         }
  413.         require(p->lock!=NULL, "rJunc: lock array is NULL");
  414.         if ( p->lock[k] ) return NULL;
  415.         /* if no FOLLOW assume k EOF's */
  416.         if ( p->p1 == NULL ) return eofnode(k);
  417.         p->lock[k] = TRUE;
  418.     }
  419.  
  420.     if ( p->p2 == NULL )
  421.     {
  422.         TRAV(p->p1, k, rk,t);
  423.         if ( p->jtype==EndRule ) p->lock[k]=FALSE;
  424.         return t;
  425.     }
  426.     TRAV(p->p1, k, rk, t);
  427.     if ( p->jtype!=RuleBlk ) TRAV(p->p2, k, rk, u);
  428.     if ( p->jtype==EndRule ) p->lock[k] = FALSE;/* unlock node */
  429.  
  430.     if ( t==NULL ) return tmake(tnode(ALT), u, NULL);
  431.     return tmake(tnode(ALT), t, u, NULL);
  432. }
  433.  
  434. Tree *
  435. tRuleRef(p,k,rk_out)
  436. RuleRefNode *p;
  437. int k;
  438. set *rk_out;
  439.     int k2;
  440.     Tree *t, *u;
  441.     Junction *r;
  442.     set rk, rk2;
  443.     char save_halt;
  444.     RuleEntry *q = (RuleEntry *) hash_get(Rname, p->text);
  445.     
  446. #ifdef DBG_TRAV
  447.     fprintf(stderr, "tRuleRef: %s\n", p->text);
  448. #endif
  449.     if ( q == NULL )
  450.     {
  451.         TRAV(p->next, k, rk_out, t);/* ignore undefined rules */
  452.         return t;
  453.     }
  454.     rk = rk2 = empty;
  455.     r = RulePtr[q->rulenum];
  456.     if ( r->lock[k] ) return NULL;
  457.     save_halt = r->end->halt;
  458.     r->end->halt = TRUE;        /* don't let reach fall off end of rule here */
  459.     TRAV(r, k, &rk, t);
  460.     r->end->halt = save_halt;
  461. #ifdef DBG_TREES
  462.     fprintf(stderr, "after ruleref, t is:"); preorder(t); fprintf(stderr, "\n");
  463. #endif
  464.     t = tshrink( t );
  465.     while ( !set_nil(rk) ) {    /* any k left to do? if so, link onto tree */
  466.         k2 = set_int(rk);
  467.         set_rm(k2, rk);
  468.         TRAV(p->next, k2, &rk2, u);
  469.         t = tlink(t, u, k2);    /* any alts missing k2 toks, add u onto end */
  470.     }
  471.     set_orin(rk_out, rk2);        /* remember what we couldn't do */
  472.     set_free(rk2);
  473.     return t;
  474. }
  475.  
  476. Tree *
  477. tToken(p,k,rk)
  478. TokNode *p;
  479. int k;
  480. set *rk;
  481. {
  482.     Tree *t;
  483.     require(constrain>=fset&&constrain<=&(fset[LL_k]),"tToken: constrain is not a valid set");
  484.  
  485.     constrain = &fset[maxk-k+1];
  486.  
  487. #ifdef DBG_TRAV
  488.     fprintf(stderr, "tToken(%d): %s\n", k, TerminalString(p->token));
  489.     if ( ConstrainSearch ) {
  490.         fprintf(stderr, "constrain is:"); s_fprT(stderr, *constrain); fprintf(stderr, "\n");
  491.     }
  492. #endif
  493.     if ( ConstrainSearch )
  494.         if ( !set_el(p->token, *constrain) )
  495.         {
  496. /*            fprintf(stderr, "ignoring token %s(%d)\n", TerminalString(p->token),
  497.                             k);*/
  498.             return NULL;
  499.         }
  500.     if ( k == 1 ) return tnode(p->token);
  501.     TRAV(p->next, k-1, rk, t);
  502.     /* here, we are positive that, at least, this tree will not contribute
  503.      * to the LL(2) tree since it will be too shallow, IF t==NULL
  504.      */
  505.     if ( t == NULL )    /* tree will be too shallow */
  506.     {
  507.         return NULL;
  508.     }
  509. #ifdef DBG_TREES
  510.     fprintf(stderr, "tToken(%d)->next:",k); preorder(t); fprintf(stderr, "\n");
  511. #endif
  512.     return tmake(tnode(p->token), t, NULL);
  513. }
  514.  
  515. Tree *
  516. tAction(p,k,rk)
  517. ActionNode *p;
  518. int k;
  519. set *rk;
  520. {
  521.     Tree *t;
  522.     
  523.     /*fprintf(stderr, "tAction\n");*/
  524.     TRAV(p->next, k, rk, t);
  525.     return t;
  526. }
  527.  
  528. /* see if e exists in s as a possible input permutation (e is always a chain) */
  529. int
  530. tmember(e,s)
  531. Tree *e, *s;
  532. {
  533.     if ( e==NULL||s==NULL ) return 0;
  534.     /*fprintf(stderr, "tmember(");
  535.     preorder(e); fprintf(stderr, ",");
  536.     preorder(s); fprintf(stderr, " )\n");*/
  537.     if ( s->token == ALT && s->right == NULL ) return tmember(e, s->down);
  538.     if ( e->token!=s->token )
  539.     {
  540.         if ( s->right==NULL ) return 0;
  541.         return tmember(e, s->right);
  542.     }
  543.     if ( e->down==NULL && s->down == NULL ) return 1;
  544.     if ( tmember(e->down, s->down) ) return 1;
  545.     if ( s->right==NULL ) return 0;
  546.     return tmember(e, s->right);
  547. }
  548.  
  549. /* combine (? (A t) ... (A u) ...) into (? (A t u)) */
  550. Tree *
  551. tleft_factor(t)
  552. Tree *t;
  553. {
  554.     Tree *u, *v, *trail, *w;
  555.  
  556.     /* left-factor what is at this level */
  557.     if ( t == NULL ) return NULL;
  558.     for (u=t; u!=NULL; u=u->right)
  559.     {
  560.         trail = u;
  561.         v=u->right;
  562.         while ( v!=NULL )
  563.         {
  564.             if ( u->token == v->token )
  565.             {
  566.                 if ( u->down!=NULL )
  567.                 {
  568.                     for (w=u->down; w->right!=NULL; w=w->right) {;}
  569.                     w->right = v->down;    /* link children together */
  570.                 }
  571.                 else u->down = v->down;
  572.                 trail->right = v->right;        /* unlink factored node */
  573.                 tfree( v );
  574.                 v = trail->right;
  575.             }
  576.             else {trail = v; v=v->right;}
  577.         }
  578.     }
  579.     /* left-factor what is below */
  580.     for (u=t; u!=NULL; u=u->right) u->down = tleft_factor( u->down );
  581.     return t;
  582. }
  583.  
  584. /* remove the permutation p from t if present */
  585. Tree *
  586. trm_perm(t,p)
  587. Tree *t, *p;
  588. {
  589.     /*
  590.     fprintf(stderr, "trm_perm(");
  591.     preorder(t); fprintf(stderr, ",");
  592.     preorder(p); fprintf(stderr, " )\n");
  593.     */
  594.     if ( t == NULL || p == NULL ) return NULL;
  595.     if ( t->token == ALT )
  596.     {
  597.         t->down = trm_perm(t->down, p);
  598.         if ( t->down == NULL )                 /* nothing left below, rm cur node */
  599.         {
  600.             Tree *u = t->right;
  601.             tfree( t );
  602.             return trm_perm(u, p);
  603.         }
  604.         t->right = trm_perm(t->right, p);    /* look for more instances of p */
  605.         return t;
  606.     }
  607.     if ( p->token != t->token )                /* not found, try a sibling */
  608.     {
  609.         t->right = trm_perm(t->right, p);
  610.         return t;
  611.     }
  612.     t->down = trm_perm(t->down, p->down);
  613.     if ( t->down == NULL )                     /* nothing left below, rm cur node */
  614.     {
  615.         Tree *u = t->right;
  616.         tfree( t );
  617.         return trm_perm(u, p);
  618.     }
  619.     t->right = trm_perm(t->right, p);        /* look for more instances of p */
  620.     return t;
  621. }
  622.  
  623. /* add the permutation 'perm' to the LL_k sets in 'fset' */
  624. void
  625. tcvt(fset,perm)
  626. set *fset;
  627. Tree *perm;
  628. {
  629.     if ( perm==NULL ) return;
  630.     set_orel(perm->token, fset);
  631.     tcvt(fset+1, perm->down);
  632. }
  633.  
  634. /* for each element of ftbl[k], make it the root of a tree with permute(ftbl[k+1])
  635.  * as a child.
  636.  */
  637. Tree *
  638. permute(k)
  639. int k;
  640. {
  641.     Tree *t, *u;
  642.     
  643.     if ( k>LL_k ) return NULL;
  644.     if ( ftbl[k][index[k]] == nil ) return NULL;
  645.     t = permute(k+1);
  646.     if ( t==NULL&&k<LL_k )        /* no permutation left below for k+1 tokens? */
  647.     {
  648.         index[k+1] = 0;
  649.         (index[k])++;            /* try next token at this k */
  650.         return permute(k);
  651.     }
  652.     
  653.     u = tmake(tnode(ftbl[k][index[k]]), t, NULL);
  654.     if ( k == LL_k ) (index[k])++;
  655.     return u;
  656. }
  657.  
  658. /* Compute LL(k) trees for alts alt1 and alt2 of p.
  659.  * function result is tree of ambiguous input permutations
  660.  *
  661.  * ALGORITHM may change to look for something other than LL_k size
  662.  * trees ==> maxk will have to change.
  663.  */
  664. Tree *
  665. VerifyAmbig(alt1,alt2,ft,fs,t,u,numAmbig)
  666. Junction *alt1, *alt2;
  667. unsigned **ft;
  668. set *fs;                        /* set of possible ambiguities */
  669. Tree **t, **u;                    /* return constrained perm sets */
  670. int *numAmbig;                    /* how many ambigs were there */
  671. {
  672.     set rk;
  673.     Tree *perm, *ambig=NULL;
  674.     int k;
  675.  
  676.     maxk = LL_k;                /* NOTE: for now, we look for LL_k */
  677.     ftbl = ft;
  678.     fset = fs;
  679.     constrain = &(fset[1]);
  680.     index = (int *) calloc(LL_k+1, sizeof(int));
  681.     if ( index == NULL )
  682.     {
  683.         fprintf(stderr, ErrHdr, CurAmbigfile, CurAmbigline);
  684.         fprintf(stderr, " out of memory while analyzing alts %d and %d of %s\n",
  685.                         CurAmbigAlt1,
  686.                         CurAmbigAlt2,
  687.                         CurAmbigbtype);
  688.         exit(-1);
  689.     }
  690.     for (k=1; k<=LL_k; k++) index[k] = 0;
  691.  
  692.     rk = empty;
  693.     ConstrainSearch = 1;    /* consider only tokens in ambig sets */
  694.  
  695.     TRAV(alt1->p1, LL_k, &rk, *t);
  696.     *t = tshrink( *t );
  697.     *t = tflatten( *t );
  698.     *t = prune(*t, LL_k);
  699.     *t = tleft_factor( *t );
  700. /*    fprintf(stderr, "after shrink&flatten&prune&left_factor:"); preorder(*t); fprintf(stderr, "\n");*/
  701.     if ( *t == NULL )
  702.     {
  703. /*        fprintf(stderr, "TreeIncomplete --> no LL(%d) ambiguity\n", LL_k);*/
  704.         Tfree( *t );    /* kill if impossible to have ambig */
  705.         *t = NULL;
  706.     }
  707.  
  708.     TRAV(alt2->p1, LL_k, &rk, *u);
  709.     *u = tshrink( *u );
  710.     *u = tflatten( *u );
  711.     *u = prune(*u, LL_k);
  712.     *u = tleft_factor( *u );
  713. /*    fprintf(stderr, "after shrink&flatten&prune&lfactor:"); preorder(*u); fprintf(stderr, "\n");*/
  714.     if ( *u == NULL )
  715.     {
  716. /*        fprintf(stderr, "TreeIncomplete --> no LL(%d) ambiguity\n", LL_k);*/
  717.         Tfree( *u );
  718.         *u = NULL;
  719.     }
  720.  
  721.     for (k=1; k<=LL_k; k++) set_clr( fs[k] );
  722.  
  723.     ambig = tnode(ALT);
  724.     k = 0;
  725.     if ( *t!=NULL && *u!=NULL )
  726.     {
  727.         while ( (perm=permute(1))!=NULL )
  728.         {
  729. /*            fprintf(stderr, "chk perm:"); preorder(perm); fprintf(stderr, "\n");*/
  730.             if ( tmember(perm, *t) && tmember(perm, *u) )
  731.             {
  732. /*                fprintf(stderr, "ambig upon"); preorder(perm); fprintf(stderr, "\n");*/
  733.                 k++;
  734.                 perm->right = ambig->down;
  735.                 ambig->down = perm;
  736.                 tcvt(&(fs[1]), perm);
  737.             }
  738.             else Tfree( perm );
  739.         }
  740.     }
  741.  
  742.     *numAmbig = k;
  743.     if ( ambig->down == NULL ) {tfree(ambig); ambig = NULL;}
  744.     free( index );
  745.     /*fprintf(stderr, "final ambig:"); preorder(ambig); fprintf(stderr, "\n");*/
  746.     return ambig;
  747. }
  748.